﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Language.Stub;

namespace Red.Scripts.Options.Tool.Pane.FindSymbol
{
    public partial class Control : UserControl
    {
        #region Dependency Properties

        public static readonly DependencyProperty FlatDBProperty =
        DependencyProperty.Register( "FlatDB", typeof( IEnumerable<Symbol> ), typeof( Control ) );

        #endregion Dependency Properties

        #region Public Properties

        public IEnumerable<Symbol> FlatDB
        {
            get { return (IEnumerable<Symbol>)GetValue( FlatDBProperty ); }
            set { SetValue( FlatDBProperty, value ); }
        }

        #endregion Public Properties

        #region C-Tor

        /// <summary>
        /// Initializes a new instance of the <see cref="Control"/> class.
        /// </summary>
        public Control()
        {
            InitializeComponent();
        }

        #endregion C-Tor

        #region Event Handlers

        private void RowMouseDoubleClick( object sender, MouseButtonEventArgs e )
        {
            Symbol symbol = (Symbol)( (FrameworkElement)sender ).DataContext;
            GoToSymbol?.Invoke( symbol );
        }

        private void RowKeyDown( object sender, KeyEventArgs e )
        {
            if ( e.Key == Key.Return )
            {
                Symbol symbol = (Symbol)( (FrameworkElement)sender ).DataContext;
                GoToSymbol?.Invoke( symbol );

                e.Handled = true;
            }
        }

        #endregion Event Handlers

        #region Events

        public delegate void GoToSymbolHandler( Symbol symbol );
        public event GoToSymbolHandler GoToSymbol;

        #endregion Events
    }
}
